﻿using System;
using System.Drawing;
using System.Windows.Forms;
using CommonLibraries.DeviceLibrary;
using System.Threading;
using System.IO;
using CommonLibraries.DeviceLibrary.Helpers;

namespace demo
{
    public partial class BaseForm : Form
    {
        protected GPS m_GPS = GPS.GetInstance();
        protected Camera m_Camera = Camera.GetInstance();
        protected Handheld m_Battery = Handheld.GetInstance();
        protected Compass m_Compass = Compass.GetInstance();
        
        Int32 m_NoSignalCounter = 5;
        public bool m_ShowStatusMenu = true;
        private int CounterStopGPS = 0;


        public int SetCameraStatus 
        {
            set { pbxCameraStatus.Image = SetStatus(value); }
        }
        public int SetGPSStatus
        {
            set { pbxGPSstatus.Image = SetStatus(value); }
        }
        public int SetSolutionStatus
        {
            set { pbxSolutionStatus.Image = SetStatus(value); }
        }
        public int SetBatteryStatus
        {
            set { pbxBatteryStatus.Image = SetStatus(value); }
        }

        public BaseForm()
        {
            InitializeComponent();
        }
        void BaseForm_Load(object sender, System.EventArgs e)
        {
            if(!m_ShowStatusMenu)
                HideShowStatusMenu();

            if (!isDLLInstalled())
                this.Close();

            //power on GPS if off
            GPSOn();
            
            //Power on Camera, if off
            CameraOn();

            //m_GPS_HasSolutionChanged(null, null);
            m_GPS.HasSolutionChanged += new EventHandler(m_GPS_HasSolutionChanged);

            SetSolutionStatus = (int)SquareColors.Red;
            this.buttonFocus.Focus();
            refreshTime();

        }
        private bool isDLLInstalled()
        {
            if (!File.Exists(".//Windows//TGMS2HELPER.dll"))
            {
                MessageBox.Show(Constants.K_MissingTGMS2HELPER);
                return false;
            }
            return true;
        }

        private void refreshTime()
        {
            string _TempTime;
            if (!m_GPS.HasSolution)
                _TempTime = System.DateTime.Now.ToString("dd/MM/yy") + " " + System.DateTime.Now.ToShortTimeString();
            else
                _TempTime = m_GPS.NmeaGGA.SateliteTime.ToString();
            this.transparentLabelTimeDate.Text = _TempTime;
        }

        #region StatusFunctions
        protected Image SetStatus(int status)
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(BaseForm));
            string backgroundImage = "";
            switch (status)
            {
                case 3:
                    backgroundImage = "Indicator_White";
                    break;
                case 2:
                    backgroundImage = "Indicator_Yellow";
                    break;
                case 1:
                    backgroundImage = "Indicator_Green";
                    break;
                case 0:
                    backgroundImage = "Indicator_Red";
                    break;
            }
            return ((System.Drawing.Image)(resources.GetObject(backgroundImage)));
        }
        #endregion

        #region GPS
        protected void GPSOn()
        {
            if (m_GPS.PowerState == PowerStates.Off)
            {
                m_GPS.Reset();
                SetGPSStatus = (int)SquareColors.Red;
                m_GPS.PowerState = PowerStates.On;
                SetGPSStatus = (int)SquareColors.Green;
            }
            else
                SetGPSStatus = (int)SquareColors.Green;
        }

        private void CheckGPSPowerState()
        {
            GPSOn();
        }
        private delegate void ThreadDelegate();
        protected void HasSolutionChanged()
        {
            try
            {
                if (!m_GPS.HasSolution)
                {
                    if (m_NoSignalCounter == 0)
                    {
                        SetSolutionStatus = (int)SquareColors.Red;
                    }
                    else
                        m_NoSignalCounter--;
                }
                else
                {
                    SetSolutionStatus = (int)SquareColors.Green;
                    m_NoSignalCounter = 5;
                    refreshTime();
                }
                CheckGPSPowerState();
                CheckCameraPowerState();
                CheckBattery();
               

            }
            catch (Exception ex)
            {
                ErrorManager.CreateDebug("HasSolutionChanged->Problems\n" + ex.Message);
            }
        }
        public void m_GPS_HasSolutionChanged(object sender, EventArgs e)
        {
            try
            {
                this.Invoke(new ThreadDelegate(this.HasSolutionChanged));
            }
            catch (ObjectDisposedException ex)
            {
                ErrorManager.CreateDebug("BeginInvoke->"+ex.Message);
            }
        }

        #endregion

        #region CAMERA
        protected void CameraOn()
        {
            if (m_Camera.PowerState == PowerStates.Off)
            {
                SetCameraStatus = (int)SquareColors.Green;
                m_Camera.PowerState = PowerStates.On;
                SetCameraStatus = (int)SquareColors.Red;
            }
            else
                SetCameraStatus = (int)SquareColors.Green;
        }

        private void CheckCameraPowerState()
        {
            CameraOn();
        }
        #endregion


        #region Battery

        private void CheckBatteryLevel()
        {
            int _MaxLoad = 10000;
            Int32 _BatteryLevel = m_Battery.BatteryLevel;

            int _CurrentLoadPercentage =  (_BatteryLevel * 100)/_MaxLoad;

            if (_CurrentLoadPercentage >= 51 && _CurrentLoadPercentage <= 100)
                SetBatteryStatus = (int)SquareColors.Green;
            else if (_CurrentLoadPercentage < 51 && _CurrentLoadPercentage > 25)
                SetBatteryStatus = (int)SquareColors.Yellow;
            else
                SetBatteryStatus = (int)SquareColors.Red;

        }
        private void CheckBattery()
        {
            if (m_Battery.PowerSource == PowerSources.Battery)
                CheckBatteryLevel();
            else
                SetBatteryStatus = (int)SquareColors.White;
        }
        
        #endregion


        public virtual void buttonFocus_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
        {
        }

        public void HideShowStatusMenu()
        {
            this.pbxBatteryStatus.Visible = false;
            this.pbxCameraStatus.Visible = false;
            this.pbxGPSstatus.Visible = false;
            this.pbxSolutionStatus.Visible = false;

            this.transparentLabelBattery.Visible = false;
            this.transparentLabelCamera.Visible = false;
            this.transparentLabelGPS.Visible = false;
            this.transparentLabelSolution.Visible = false;
            this.transparentLabelTimeDate.Visible = false;
        }

        public virtual void BaseForm_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            
        }

        private void transparentLabelSolution_Click(object sender, EventArgs e)
        {
            if (m_GPS.HasSolution)
                SetSolutionStatus = (int)SquareColors.Green;
            else
                SetSolutionStatus = (int)SquareColors.Red;


        }



        void timerCheckSolution_Tick(object sender, System.EventArgs e)
        {
            if (CounterStopGPS == 1)
            {
                m_GPS.Restart();
                CounterStopGPS = 0;
            
            }
            else
            {
                m_GPS.Stop();
                CounterStopGPS++;
            }
        }
    }
}